package test.methodhandle;


import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * @author yachao.guo
 * @date 2019-06-05 11:24
 */
public class MethodHandleDemo {

    public static void main(String[] args)throws Throwable {
//        MethodType methodType = MethodType.methodType(void.class,String.class);
//        MethodHandle methodHandle  = MethodHandles.lookup().findVirtual(Test.class,"testMethod",methodType);
//        methodHandle.invoke(Test.class.newInstance(),"hello world");
//
//
//        MethodHandle methodHandle1  = MethodHandles.lookup().findVirtual(Test.class,"testMethod",MethodType.methodType(Object.class,String.class,int.class));
//        methodHandle1.invoke(Test.class.newInstance(),"hello world",5);

        MethodType methodTyp2 =MethodType.methodType(int.class,int.class,int.class);

        MethodHandle methodHandle2  = MethodHandles.lookup().findStatic(Test.class,"add",methodTyp2);
        methodHandle2.invokeExact(6,5);

    }




}
class Test{

    public void testMethod(String str) {
        System.out.println("testMethod : "+str);
    }

    public Object testMethod(String str, int x) {
        System.out.println("string = " + str+ " , int = " + x);
        return x;
    }

    public static int add(int a, int b) {
        System.out.println("a + b = " + (a + b));
        return a + b;
    }

}